package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.app.dao.BsMemberDao;
import io.renren.modules.app.dao.BsRegisgooutDao;
import io.renren.modules.app.entity.BsMemberEntity;
import io.renren.modules.app.entity.BsProtocolEntity;
import io.renren.modules.app.entity.BsRegisgooutEntity;
import io.renren.modules.app.service.BsRegisgooutService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;


@Service("bsRegisgooutService")
@Slf4j
public class BsRegisgooutServiceImpl extends ServiceImpl<BsRegisgooutDao, BsRegisgooutEntity> implements BsRegisgooutService {

    @Resource
    private BsRegisgooutDao bsRegisgooutDao ;
    @Resource
    private BsMemberDao bsMemberDao ;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        LambdaQueryWrapper<BsRegisgooutEntity> wrapper1 = new LambdaQueryWrapper<>();

        if (params.get("memberName") != null && !params.get("memberName").toString().equals("")){
            LambdaQueryWrapper<BsMemberEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(BsMemberEntity::getMemberName,params.get("memberName"));
            List<BsMemberEntity> bsMemberEntities = bsMemberDao.selectList(queryWrapper);
            List<String> list = new ArrayList<>();
            if (bsMemberEntities.size() > 0){
                for (BsMemberEntity bsMemberEntity : bsMemberEntities) {
                    list.add(bsMemberEntity.getMemberId());
                }
                wrapper1.in(BsRegisgooutEntity::getMemberId,list);
            }else{
                IPage<BsRegisgooutEntity> page = this.page(
                        new Query<BsRegisgooutEntity>().getPage(params),
                        new LambdaQueryWrapper<BsRegisgooutEntity>().eq(BsRegisgooutEntity::getId,-1)
                );
                return new PageUtils(page);
            }

        }
        if (params.get("gooutType") != null && !params.get("gooutType").toString().equals("")){
            wrapper1.eq(BsRegisgooutEntity::getGooutType,params.get("gooutType"));
        }
        if (params.get("gooutDateBegin") != null && !params.get("gooutDateBegin").toString().equals("")){
            wrapper1.ge(BsRegisgooutEntity::getGooutDate,params.get("gooutDateBegin"));
        }
        if (params.get("gooutDateEnd") != null && !params.get("gooutDateEnd").toString().equals("")){
            wrapper1.le(BsRegisgooutEntity::getGooutDate,params.get("gooutDateEnd"));
        }


        IPage<BsRegisgooutEntity> page = this.page(
                new Query<BsRegisgooutEntity>().getPage(params),
                wrapper1
        );

        for (BsRegisgooutEntity record : page.getRecords()) {
            //会员姓名数据清洗
            LambdaQueryWrapper<BsMemberEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BsMemberEntity::getMemberId,record.getMemberId());
            List<BsMemberEntity> bsMemberEntities = bsMemberDao.selectList(wrapper);
            if (bsMemberEntities.size() == 1){
                record.setMemberName(bsMemberEntities.get(0).getMemberName());
            }
        }

        return new PageUtils(page);
    }

    @Override
    public void saveRegis(BsRegisgooutEntity bsRegisgoout) {
        try {
            //会员编号存在性校验
            LambdaQueryWrapper<BsMemberEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BsMemberEntity::getMemberId,bsRegisgoout.getMemberId());
            List<BsMemberEntity> entities = bsMemberDao.selectList(wrapper);
            if (entities.size() < 1){
                throw new RRException("会员编号不合法");
            }
            if (bsRegisgoout.getGooutIdnum().length() != 18){
                throw new RRException("身份证号长度不合法");
            }
            //电话号校验
            if (!phoneValidate(bsRegisgoout.getGooutPhone())){
                throw new RRException("电话号不合法");
            }
            Date gooutDate = bsRegisgoout.getGooutDate();//外出时间
            Date gooutPlandate = bsRegisgoout.getGooutPlandate();//计划返回时间
            Date gooutFactdate = bsRegisgoout.getGooutFactdate();//实际返回时间
            if (gooutDate.getTime() > gooutPlandate.getTime()){
                throw new RRException("外出时间不能大于计划返回时间");
            }
            if (gooutDate.getTime() > gooutFactdate.getTime()){
                throw new RRException("外出时间不能大于实际返回时间");
            }
            bsRegisgooutDao.insert(bsRegisgoout);
        }catch (Exception e){
            log.info("报错所在的类及方法：{}","BsRegisgooutService --> saveRegis");
            log.info(e.getMessage());
            throw new RRException(e.getMessage());
        }
    }

    @Override
    public void updateRegisById(BsRegisgooutEntity bsRegisgoout) {
        try {
            BsRegisgooutEntity entity = bsRegisgooutDao.selectById(bsRegisgoout.getId());
            if (!entity.getMemberId().equals(bsRegisgoout.getMemberId())){
                throw new RRException("会员编号不可修改");
            }
            if (bsRegisgoout.getGooutIdnum().length() != 18){
                throw new RRException("身份证号长度不合法");
            }
            //电话号校验
            if (!phoneValidate(bsRegisgoout.getGooutPhone())){
                throw new RRException("电话号不合法");
            }
            Date gooutDate = bsRegisgoout.getGooutDate();//外出时间
            Date gooutPlandate = bsRegisgoout.getGooutPlandate();//计划返回时间
            Date gooutFactdate = bsRegisgoout.getGooutFactdate();//实际返回时间
            if (gooutDate.getTime() > gooutPlandate.getTime()){
                throw new RRException("外出时间不能大于计划返回时间");
            }
            if (gooutDate.getTime() > gooutFactdate.getTime()){
                throw new RRException("外出时间不能大于实际返回时间");
            }
            bsRegisgooutDao.updateById(bsRegisgoout);
        }catch (Exception e){
            log.info("报错所在的类及方法：{}","BsRegisgooutService --> updateRegisById");
            log.info(e.getMessage());
            throw new RRException(e.getMessage());
        }
    }

    //校验电话号公共方法
    private boolean phoneValidate(String phoneNumber){
        if (phoneNumber != null && !phoneNumber.isEmpty()){
            boolean matches = Pattern.matches(
                    "^1[3-9]\\d{9}$|" +
                            "^1[3-9]\\d{1}[-\\s]\\d{4}[-\\s]\\d{4}$|" +
                            "^\\(1[3-9]\\d{1}\\)\\d{4}-\\d{4}$|" +
                            "^(?:\\(\\+\\d{2}\\)|\\+\\d{2})(\\d{11})$|" +
                            "^0\\d{3}-\\d{7}$|" +
                            "^0\\d{2}-\\d{8}$", phoneNumber);

            return matches;
        }
        return false;
    }

}
